package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class AddPage extends Utils {
    public static String url="http://43.138.73.175:8080/book_add.html";
    public AddPage(){
        super(url);
    }

    public void AddPageFail() throws IOException {
        //通过查看页面元素观察是否加载完成列表页
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("body > div > div > h2 > span"));
        driver.findElement(By.cssSelector("#bookName")).sendKeys("测试书");
        driver.findElement(By.cssSelector("#bookAuthor")).sendKeys("测试作者");
        driver.findElement(By.cssSelector("#bookPrice")).sendKeys("46.5");
        driver.findElement(By.cssSelector("#bookPublisher")).sendKeys("测试出版社");
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#addBook > div:nth-child(7) > button.btn.btn-info.btn-lg")).click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String alertText = alert.getText();
        alert.accept();
        getScreenShot(getClass().getName());
        assert alertText.equals("添加失败:null");
    }
    public void AddPageSuc() throws IOException {
        driver.findElement(By.cssSelector("#bookStock")).sendKeys("12");
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#addBook > div:nth-child(7) > button.btn.btn-info.btn-lg")).click();
    }
}
